package com.bsoft.gol.hcs.utils;

import com.bsoft.gol.hcs.clinical.bean.response.inspectionResq.QueryInspectionItemListNewResp;
import com.bsoft.gol.hcs.clinical.bean.response.inspectionresponse.LabRequestQueryItemsResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ctd.util.xml.jaxb.JAXBUtils;
import hcn.util.BaseResponse;
import org.dom4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.atag.util.http.KdHttpClientUtil;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xhf
 * @date 2019/11/14 18:46
 * @Description
 */
public class WhisClientUtil {
    private final static Logger logger = LoggerFactory.getLogger(WhisClientUtil.class);
    // webservice的wsdl地址
//    private static final String WSDLURL = "http://10.10.2.85:8089/mrc/ws/service.ws?wsdl";
    private static final String WSDLURL = "http://10.8.3.125:8089/mrc/ws/service.ws?wsdl";

    //测试方法
    public static void main(String[] args) {
        JSONObject jSONObjectData = null;
        String xml = "<?xml version=\"1.0\" ?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:invokeResponse xmlns:ns2=\"http://ws.access.hai/\"><return>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;BSXml&gt;&lt;MsgHeader&gt;&lt;Organization&gt;1&lt;/Organization&gt;&lt;Sender&gt;GOL&lt;/Sender&gt;&lt;MsgType&gt;ODS_03050001_SUC&lt;/MsgType&gt;&lt;MsgVersion&gt;4.0&lt;/MsgVersion&gt;&lt;/MsgHeader&gt;&lt;MsgBody&gt;&lt;Status&gt;true&lt;/Status&gt;&lt;Code&gt;200&lt;/Code&gt;&lt;Detail&gt;查询成功&lt;/Detail&gt;&lt;Data&gt;&lt;DataLists&gt;&lt;DataList&gt;&lt;ItemTypeCode&gt;1&lt;/ItemTypeCode&gt;&lt;ItemTypeName&gt;生化系列&lt;/ItemTypeName&gt;&lt;Items&gt;&lt;Item&gt;&lt;ItemCode&gt;6709&lt;/ItemCode&gt;&lt;ItemName&gt;白细胞计数(WBC)&lt;/ItemName&gt;&lt;InputCode&gt;BXBJS&lt;/InputCode&gt;&lt;ComposeType&gt;0&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;2&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;0&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;末段尿&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;2008&lt;/ItemCode&gt;&lt;ItemName&gt;生化全套&lt;/ItemName&gt;&lt;InputCode&gt;SHQT&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;139.5&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;Q&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血清&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;12466&lt;/ItemCode&gt;&lt;ItemName&gt;常规项目&lt;/ItemName&gt;&lt;InputCode&gt;CGXM&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;98&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;/Items&gt;&lt;/DataList&gt;&lt;DataList&gt;&lt;ItemTypeCode&gt;2&lt;/ItemTypeCode&gt;&lt;ItemTypeName&gt;免疫系列&lt;/ItemTypeName&gt;&lt;Items&gt;&lt;Item&gt;&lt;ItemCode&gt;2009&lt;/ItemCode&gt;&lt;ItemName&gt;甲肝+丙肝&lt;/ItemName&gt;&lt;InputCode&gt;JGBG&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;189&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;Q&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血清&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;2010&lt;/ItemCode&gt;&lt;ItemName&gt;单纯疱疹I+II&lt;/ItemName&gt;&lt;InputCode&gt;DCPZII&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;470&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;Q&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血清&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;/Items&gt;&lt;/DataList&gt;&lt;DataList&gt;&lt;ItemTypeCode&gt;3&lt;/ItemTypeCode&gt;&lt;ItemTypeName&gt;临检系列&lt;/ItemTypeName&gt;&lt;Items&gt;&lt;Item&gt;&lt;ItemCode&gt;1242&lt;/ItemCode&gt;&lt;ItemName&gt;血常规&lt;/ItemName&gt;&lt;InputCode&gt;XCG&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;320&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;/Items&gt;&lt;/DataList&gt;&lt;DataList&gt;&lt;ItemTypeCode&gt;4&lt;/ItemTypeCode&gt;&lt;ItemTypeName&gt;急诊项目&lt;/ItemTypeName&gt;&lt;Items/&gt;&lt;/DataList&gt;&lt;DataList&gt;&lt;ItemTypeCode&gt;5&lt;/ItemTypeCode&gt;&lt;ItemTypeName&gt;微生物&lt;/ItemTypeName&gt;&lt;Items&gt;&lt;Item&gt;&lt;ItemCode&gt;1206&lt;/ItemCode&gt;&lt;ItemName&gt;痰培养及鉴定&lt;/ItemName&gt;&lt;InputCode&gt;ZJPYJJD&lt;/InputCode&gt;&lt;ComposeType&gt;0&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;30&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;K&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;痰&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1235&lt;/ItemCode&gt;&lt;ItemName&gt;结核检测&lt;/ItemName&gt;&lt;InputCode&gt;JHJC&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;38&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1236&lt;/ItemCode&gt;&lt;ItemName&gt;血需氧+厌氧菌培养及鉴定&lt;/ItemName&gt;&lt;InputCode&gt;XXYYYJ&lt;/InputCode&gt;&lt;ComposeType&gt;2&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1244&lt;/ItemCode&gt;&lt;ItemName&gt;血液培养1&lt;/ItemName&gt;&lt;InputCode&gt;XYPY1&lt;/InputCode&gt;&lt;ComposeType&gt;2&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1245&lt;/ItemCode&gt;&lt;ItemName&gt;尿培养&lt;/ItemName&gt;&lt;InputCode&gt;NPY&lt;/InputCode&gt;&lt;ComposeType&gt;2&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1246&lt;/ItemCode&gt;&lt;ItemName&gt;血培养&lt;/ItemName&gt;&lt;InputCode&gt;XPY&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;125&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;Item&gt;&lt;ItemCode&gt;1247&lt;/ItemCode&gt;&lt;ItemName&gt;血厌氧培养及鉴定&lt;/ItemName&gt;&lt;InputCode&gt;XYYPYJ&lt;/InputCode&gt;&lt;ComposeType&gt;1&lt;/ComposeType&gt;&lt;ItemCount&gt;1&lt;/ItemCount&gt;&lt;ItemPrice&gt;25&lt;/ItemPrice&gt;&lt;SampleTypeCode&gt;C&lt;/SampleTypeCode&gt;&lt;SampleTypeName&gt;血液&lt;/SampleTypeName&gt;&lt;IsEmergency&gt;0&lt;/IsEmergency&gt;&lt;/Item&gt;&lt;/Items&gt;&lt;/DataList&gt;&lt;/DataLists&gt;&lt;/Data&gt;&lt;/MsgBody&gt;&lt;/BSXml&gt;\n" +
                "</return></ns2:invokeResponse></S:Body></S:Envelope>";
        try {
            Document document = DocumentHelper.parseText(xml);
            JSONObject jsonObject = elementToJSONObject(document.getRootElement());
            JSONObject body = jsonObject.getJSONArray("Body").getJSONObject(0);
            JSONObject invokeResponse = body.getJSONArray("invokeResponse").getJSONObject(0);
            String aReturn = invokeResponse.getString("return");
            List<QueryInspectionItemListNewResp> resps = new ArrayList<>();
            LabRequestQueryItemsResponse list = JAXBUtils.xml2Object(aReturn,LabRequestQueryItemsResponse.class);
//            resps.addAll(list.getDataLists().getDataList());
            System.out.println(list.getMsgBody());
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }

    public static JSONObject getJsonDataFromPhis(String webServiceUrl, String queryConditionsXml) throws IOException {
        logger.info("{}入参:{}", webServiceUrl, queryConditionsXml);
        JSONObject jSONObjectData = null;
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(webServiceUrl, "text/xml", queryConditionsXml);
        try {
            Document document = DocumentHelper.parseText(responseXML);
            JSONObject jsonObject = elementToJSONObject(document.getRootElement());
            JSONObject body = jsonObject.getJSONArray("Body").getJSONObject(0);
            JSONObject invokeResponse = body.getJSONArray("invokeResponse").getJSONObject(0);
            String aReturn = invokeResponse.getString("return");
            Document doc = DocumentHelper.parseText(aReturn);
            jSONObjectData = elementToJSONObject(doc.getRootElement());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return jSONObjectData;
    }

    public static String getStringDataFromPhis(String webServiceUrl, String queryConditionsXml) throws IOException {
        logger.info("{}入参:{}", webServiceUrl, queryConditionsXml);
        String aReturn = "";
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(webServiceUrl, "text/xml", queryConditionsXml);
        try {
            Document document = DocumentHelper.parseText(responseXML);
            JSONObject jsonObject = elementToJSONObject(document.getRootElement());
            JSONObject body = jsonObject.getJSONArray("Body").getJSONObject(0);
            JSONObject invokeResponse = body.getJSONArray("invokeResponse").getJSONObject(0);
             aReturn = invokeResponse.getString("return");
            logger.info("{}出参:{}", aReturn);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return aReturn;
    }

    public static String getDataFromPhis(String webServiceUrl, String queryConditionsXml) throws IOException {
        logger.info("{}入参:{}", webServiceUrl, queryConditionsXml);
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(webServiceUrl, "text/xml", queryConditionsXml);
        return responseXML;
    }

    public static JSONObject getJsonDataFromWhis(String webServiceUrl, String queryConditionsXml) throws IOException {
        logger.info("{}入参:{}", webServiceUrl, queryConditionsXml);
        JSONObject jSONObjectData = null;
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(webServiceUrl, "application/xml", queryConditionsXml);
        try {
            Document document = DocumentHelper.parseText(responseXML);
            jSONObjectData = elementToJSONObject(document.getRootElement());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return jSONObjectData;
    }
    public static String getJsonDataFromPlatform(String postServiceUrl, String serviceName, String msgHeaderXml, String queryConditionsXml) throws XMLStreamException, IOException {
        logger.info("service---------------------:{}", serviceName);
        logger.info("{}入参:{}", serviceName, queryConditionsXml);
        Date begin = new Date();
        // 设置编码。(因为是直接传的xml,所以我们设置为application/xml;charset=utf8)
        final String contentType = "application/xml;charset=utf8";
        /// 拼接要传递的xml数据(注意:此xml数据的模板我们根据wsdlURL从SoapUI中获得,只需要修改对应的变量值即可)
        StringBuffer xMLcontent = new StringBuffer("");
        xMLcontent.append("<BSXml>");
        xMLcontent.append(msgHeaderXml);
        xMLcontent.append(queryConditionsXml);
        xMLcontent.append("</BSXml>");
        String request = xMLcontent.toString();
        logger.info("{}完整入参:{}", serviceName, request);
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(postServiceUrl, contentType, request);
        //String jsonStr = XmlToJson.xml2jsonString(responseXML);
        logger.info("{}返回值json:{}", serviceName, responseXML);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("——————————" + simpleDateFormat.format(new Date()) + "(" + serviceName + ")耗费时间：" + (System.currentTimeMillis() - begin.getTime()) + "毫秒——————————");
        return responseXML;
    }

    public static String getSaveJsonDataFromPlatform(String postServiceUrl, String serviceName, String xml) throws XMLStreamException, IOException {
        logger.info("service---------------------:{}", serviceName);
        logger.info("{}入参:{}", serviceName, xml);
        Date begin = new Date();
        // 设置编码。(因为是直接传的xml,所以我们设置为application/xml;charset=utf8)
        final String contentType = "application/xml;charset=utf8";
        StringBuffer xMLcontent = new StringBuffer("");
        xMLcontent.append(xml);
        String request = xMLcontent.toString();
        logger.info("{}完整入参:{}", serviceName, request);
        // 调用工具类方法发送http请求
        String responseXML = KdHttpClientUtil.doXmlPost(postServiceUrl, contentType, request);
        logger.info("{}返回值json:{}", serviceName, responseXML);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("——————————" + simpleDateFormat.format(new Date()) + "(" + serviceName + ")耗费时间：" + (System.currentTimeMillis() - begin.getTime()) + "毫秒——————————");
        return responseXML;
    }

    public static BaseResponse postFromPlatform(String postServiceUrl, String serviceName, String msgHeaderXml, String queryConditionsXml) throws XMLStreamException, IOException {
        logger.info("service---------------------:{}", serviceName);
        logger.info("{}入参:{}", serviceName, queryConditionsXml);
        Date begin = new Date();
        // 设置编码。(因为是直接传的xml,所以我们设置为application/xml;charset=utf8)
        final String contentType = "application/xml;charset=utf8";
        /// 拼接要传递的xml数据(注意:此xml数据的模板我们根据wsdlURL从SoapUI中获得,只需要修改对应的变量值即可)
        StringBuffer xMLcontent = new StringBuffer("");
        xMLcontent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        xMLcontent.append("<BSXml>");
        xMLcontent.append(msgHeaderXml);
        xMLcontent.append("");
        xMLcontent.append(queryConditionsXml);
        xMLcontent.append("</BSXml>");
        String request = xMLcontent.toString();
        logger.info("{}完整入参:{}", serviceName, request);

        Map<String, String> map = new HashMap<>();
        map.put("service", serviceName);
        map.put("parameter", request);
        // 调用工具类方法发送http请求
        BaseResponse baseResponse = new BaseResponse();
        String jsonStr = "";
        baseResponse = KdHttpClientUtil.postMap(postServiceUrl, null, map);
        logger.info("{}返回值json:{}", serviceName, jsonStr);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("——————————" + simpleDateFormat.format(new Date()) + "(" + serviceName + ")耗费时间：" + (System.currentTimeMillis() - begin.getTime()) + "毫秒——————————");
        return baseResponse;
    }


    public static String mapToXml(Map<String, String> paramsMap, String root) {
        StringBuilder sb = new StringBuilder();
        sb.append("<" + root + ">");
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            String value = entry.getValue();
            if (value != null && !"".equals(value)) {
                sb.append("<").append(entry.getKey()).append(">").append(value).append("</").append(entry.getKey())
                        .append(">");
            }
        }
        sb.append("</" + root + ">");
        return sb.toString();
    }

    public static String mapToXmlWithEmptyValue(Map<String, String> paramsMap, String root) {
        StringBuilder sb = new StringBuilder();
        sb.append("<" + root + ">");
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            String value = entry.getValue();
            if (value != null ) {
                sb.append("<").append(entry.getKey()).append(">").append(value).append("</").append(entry.getKey())
                        .append(">");
            }
        }
        sb.append("</" + root + ">");
        return sb.toString();
    }

    public static String mapToXmlNull(Map<String, String> paramsMap) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            String value = entry.getValue();
            if (value != null && !"".equals(value)) {
                sb.append("<").append(entry.getKey()).append(">").append(value).append("</").append(entry.getKey())
                        .append(">");
            }
        }
        return sb.toString();
    }

    public static String mapToIsXml(Map<String, String> paramsMap, String root) {
        StringBuilder sb = new StringBuilder();
        sb.append("<" + root + ">");
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            String value = entry.getValue();
            if (value == null){
                value="";
            }
                sb.append("<").append(entry.getKey()).append(">").append(value).append("</").append(entry.getKey())
                        .append(">");

        }
        sb.append("</" + root + ">");
        return sb.toString();
    }

    public static String getHeaderXml() {
        LinkedHashMap<String, String> msgHeaderMap = new LinkedHashMap();
        msgHeaderMap.put("Sender", "CIS");
        msgHeaderMap.put("MsgType", "");
        msgHeaderMap.put("MsgVersion", "3.0");
        return mapToXml(msgHeaderMap, "MsgHeader");
    }

    public static String getMsgHeaderXml(String organization,String proSource, String msgType) {
        LinkedHashMap<String, String> msgHeaderMap = new LinkedHashMap();
        msgHeaderMap.put("Organization", organization);
        msgHeaderMap.put("Sender", proSource);
        msgHeaderMap.put("ServiceType", "service");
        msgHeaderMap.put("MsgType", msgType);
        msgHeaderMap.put("MsgVersion", "4.0");
        String SenderTime = DateUtils.dateFormate(new Date(), "yyyyMMddHHmmss");
        msgHeaderMap.put("SenderTime", SenderTime);
        return mapToXml(msgHeaderMap, "MsgHeader");
    }

    public static String getMsgRequestXml(String xml) {
        return "<BSXml>" + xml + "</BSXml>";
    }

    public static String getSoapXml(String service, String xml) {
        return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.access.hai/\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ws:invoke>\n" +
                "         <service>" + service + "</service>\n" +
                "         <urid></urid>\n" +
                "         <pwd></pwd>\n" +
                "         <parameter><![CDATA[<BSXml>" + xml + "</BSXml>]]></parameter>\n" +
                "      </ws:invoke>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
    }

    public static String getSoapXmlWithOutBSXml(String service, String xml) {
        return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.access.hai/\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ws:invoke>\n" +
                "         <service>" + service + "</service>\n" +
                "         <urid></urid>\n" +
                "         <pwd></pwd>\n" +
                "         <parameter><![CDATA[" + xml + "]]></parameter>\n" +
                "      </ws:invoke>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
    }

    public static String getMsgBody(String xml) {
        return "<MsgBody>" + xml + "</MsgBody>";
    }
    public static String getRequestXml(String msgHeaderXml, String xml) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Bsxml>" + msgHeaderXml + xml + "</Bsxml>";
    }

    /**
     * org.dom4j.Element 转  com.alibaba.fastjson.JSONObject
     *
     * @param node
     * @return
     */
    public static JSONObject elementToJSONObject(Element node) {
        JSONObject result = new JSONObject();
        // 当前节点的名称、文本内容和属性
        List<Attribute> listAttr = node.attributes();// 当前节点的所有属性的list
        for (Attribute attr : listAttr) {// 遍历当前节点的所有属性
            result.put(attr.getName(), attr.getValue());
        }
        // 递归遍历当前节点所有的子节点
        List<Element> listElement = node.elements();// 所有一级子节点的list
        if (!listElement.isEmpty()) {
            for (Element e : listElement) {// 遍历所有一级子节点
                if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
                {
                    result.put(e.getName(), e.getTextTrim());// 沒有则将当前节点作为上级节点的属性对待
                } else {
                    if (!result.containsKey(e.getName())) // 判断父节点是否存在该一级节点名称的属性
                    {
                        result.put(e.getName(), new JSONArray());// 没有则创建
                    }
                    ((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));// 将该一级节点放入该节点名称的属性对应的值中
                }
            }
        }
        return result;
    }

    public static Document strToDocument(String xml) {
        try {
            //加上xml标签是为了获取最外层的标签，如果不需要可以去掉
            return DocumentHelper.parseText("<xml>" + xml + "</xml>");
        } catch (DocumentException e) {
            return null;
        }
    }

    public static JSONObject documentToJSONObject(String xml) {
        return elementToJSONObject(strToDocument(xml).getRootElement());
    }

    public static String getAppHeaderXml() {
        LinkedHashMap<String, String> msgHeaderMap = new LinkedHashMap();
        msgHeaderMap.put("Sender", "APP");
        msgHeaderMap.put("MsgType", "ODS_2101");
        msgHeaderMap.put("MsgVersion", "3.0");
        return mapToXml(msgHeaderMap, "MsgHeader");
    }

    public static String getAppRequestXml(String xml) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Bsxml>" + xml + "</Bsxml>";
    }

    public static String getAppParameterStr(String service, String xml) {
        return "?urid=test&service=" + service + "&pwd=bsoft&parameter=" + xml;
    }

    public static Map<String, String> getMapParameterStr(String service, String xml) {
        Map<String, String> params = new HashMap<>();
        params.put("urid", "hzsdsrmyy");
        params.put("service", service);
        params.put("pwd", "bsoft");
        params.put("parameter", xml);
        return params;
    }


}

